<template>
  <div class="update-gender">
    <van-picker
      title="标题"
      show-toolbar
      :columns="columns"
      @cancel="$emit('close')"
      @confirm="onConfirm"
      :default-index="value"
      @change="onChange"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user.js';

export default {
  name: 'UpdateGender',
  props: {
    value: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      columns: ['男', '女'],
      localGender: this.value,
    };
  },
  methods: {
    async onConfirm() {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true, // 禁止背景点击
        duration: 0, // 持续展示
      });
      try {
        await updateUserProfile({ gender: this.localGender });
        this.$emit('input', this.localGender);
        this.$emit('close');
        this.$toast.success('修改成功');
      } catch (err) {
        this.$toast.fail('修改失败');
      }
    },
    onChange(picker, value, index) {
      this.localGender = index;
    },
  },
};
</script>

<style lang="less" scoped></style>
